from pickle import TRUE
from time import time
from unicodedata import name
import unittest
from numpy import choose
from selenium import webdriver
import sys
sys.path.append(r'D:\NetWork\micro-member-ui-automation')

# 导入包
from selenium.webdriver.chrome.options import Options
from Base.base_page import BasePage
from data.Config import config


#不重复打开浏览器 
chrome_options=Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver=r'C:\Users\Administrator\AppData\Local\Programs\Python\Python39\chromedriver.exe'
driver=webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
#窗口最大化
# driver.maximize_window()
#browser = webdriver.Chrome()
#print(driver.title)
# driver = webdriver.Chrome()
xp = 'xpath'


#活动管理
class LuckDraw(BasePage,unittest.TestCase):

    #选择活动
    def ClickLuckyDraw(self,activity_type):
        self.click(xp,'//span[text()="活动管理"]/../..')
        if activity_type  == '抽奖活动' :
            self.click(xp,'//span[text()="抽奖活动"]/../..')
        elif activity_type == '线下拉新活动':
            self.click(xp,'//span[text()="线下拉新活动"]/../..')
        elif activity_type == '新人送券':
            self.click(xp,'//span[text()="新人送券"]/../..')
        elif activity_type == '线上拉新活动' :
            self.click(xp,'//span[text()="线上拉新活动"]/../..')
        else :
            print('输入活动类型不存在')

    #新建抽奖活动
    def SweepStakesNewDraw(self):
        self.click(xp,'//span[text()="添加"]/..')
        self.input(xp,'//input[@placeholder="请输入活动名称"]',config.DraName)
        self.click(xp,'//div[text()="请选择活动类型"]')
        self.click(xp,config.DraTpye)
        
        #展示区域判断
        if config.AreaON == 1 :
            self.click(xp,'//span[text()=" 积分商城 "]')
            self.input(xp,'//input[@placeholder="请输入单次所需积分"]',config.integral)
        else :
            self.click(xp,'//span[text()=" 首页-活动中心 "]')
        self.click(xp,'//input[@placeholder="开始日期"]')
        stattime =  '(//table[@cellspacing="0"])[1]/tbody/tr/td[@title="'+config.Drawstartime+'"]/div'    #开始时间
        endtime = '(//table[@cellspacing="0"])[2]/tbody/tr/td[@title="'+config.Drawendtime+'"]/div'       #结束时间
        self.click(xp,stattime)
        self.click(xp,endtime)
        self.click(xp,'//a[text()="确 定"]')
        self.input(xp,'//textarea[@placeholder="请输入活动规则"]',config.rules)
        self.click(xp,'//label[@title="奖项数量"]/../../div[2]/div/span/div/div/div/div/div')  #点击奖项数量窗口
        #判断设置奖项的数量
        if config.AwardsNumber == 8:
            self.click(xp,'//li[normalize-space(text())="8"]')
        elif config.AwardsNumber == 6:
            self.click(xp,'//li[normalize-space(text())="6"]')
        else :
            self.click(xp,'//li[normalize-space(text())="4"]')
        self.click(xp,'//span[text()="设 置"]/.. ')


    #抽奖奖项每行奖项填写
    '''
    each:每行奖项,type赠送奖励类型,gold赠送的积分和储值数量,num奖品数量,
    probability概率,chance抽奖机会,sorting排序,guaranteed保底
    '''
    def EachAward(self,each,type,gold,num,name,probability,chance,sorting,guaranteed): 
        typepath =  '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[1]/div/div/div/span/span/span/span/div/div' #奖项类型的窗口
        choosepath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[1]/div/div/div/span/span/span/span[2]/i' #弹出选择优惠券窗口路径
        numpath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[2]/div/div/div/span/span/span/input'        #数量路径
        namepath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[3]/div/div/div/span/span/span/input'       #名称路径
        probabilityPath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[4]/div/div/div/span/span/span/span[2]/input'  #中奖概率路径
        if chance != None :
            chancePath =  '(//div[@style="margin-left: -5px; margin-right: -5px;"])[1]/div['+ each +']/div[5]/div/div/div/span/span/span/input'  #抽奖机会次数路径
            sortingPath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[6]/div/div/div/span/span/span/input'    #排序路径
            guaranteedPath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[7]/div/div/div/span/label/span/input '  #保底路径
        else :
            sortingPath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[5]/div/div/div/span/span/span/input'    #排序路径
            guaranteedPath = '(//div[@style="margin-left: -5px; margin-right: -5px;"])['+ each +']/div[6]/div/div/div/span/label/span/input '  #保底路径
        
        self.click(xp,typepath)

        #选择类型输入
        if type == '送积分' :
            self.TypeChoose(type)
            self.input(xp,'//input[@placeholder="请输入"]',gold)
        elif type == '送兑换券' or '送代金券'or'送满减券' :
            self.click(xp,choosepath) #点击选取窗口
            self.TypeChoose(type)
            if type == '抽奖机会' :
                self.input(xp,chancePath,chance)               #输入抽奖次数
            else :
                pass
        elif type == '无奖励' :
            self.click(xp,'//li[normalize-space(text())="无奖励"]')
        else:
            self.input(xp,'//input[@placeholder="请输入"]',gold)
        self.input(xp,numpath,num)
    
        #不是优惠券就直接输名字，优惠券和抽奖就切片名字前6个字符
        if type == '抽奖机会' or '送兑换券' or '送代金券'or'送满减券' :
            len = self.get(xp,namepath)
            self.clear(xp,namepath)
            self.input(xp,namepath,len[:6])

        else:
            self.clear(xp,namepath)
            self.input(xp,namepath,name)

        self.input(xp,probabilityPath,probability)   #输入概率
        self.input(xp,sortingPath,sorting)              #输入排序
        #是否保底
        if guaranteed == 'true':
            self.click(xp,guaranteedPath)
        else:
            pass
    
    
    #线下拉新活动
    def OfflinePullNew (self):
        self.click(xp,'//span[text()="添加"]/..')
        self.input(xp,'//input[@placeholder="活动名称最多输入25个字"]',config.OfPNName)
        #选择活动类型
        if config.OfPNType == 1 :
            self.click(xp,'//div[@title="新人特价购"]')
            self.click(xp,'//li[text()="新人优惠券"]/..')
        else:
            pass
        self.input(xp,'//input[@role="spinbutton"]',config.OfPNlimit)  #每人限领数量
        self.input(xp,'//textarea[@placeholder="请输入活动规则"]]',config.OfPNrule)     #规则
        self.click(xp,'/span[text()="保 存"][2]/..')  
        #项目设置
        self.click(xp,'(//a[text()="项目设置"])[1]')
        self.click(xp,'//span[text()="项目设置"]')
        # if config.TicketNumb > 1 :   ##需要优化多张券时
            # self.input(xp,'//input[@placeholder="请输入优惠券名称"]',config.OfPNCouponName)
            # else:
        #     self.click(xp,'/span[text()="保 存"][2]/..')
        self.click(xp,'//input[@placeholder="点击选择优惠券"]')
        self.click(xp,'//span[text()="选择"][1]')
        self.input(xp,'//input[@aria-valuemin="1"]',config.OfPNCouponNum)     #券数量
        self.input(xp,'//input[@min="0.01"]',config.OfPNSpecialAmount)       #特价
        self.input(xp,'//input[@placeholder="0不限制购"]',config.OfPNEachOf)        #限领数量
        self.click(xp,'/span[text()="保 存"][2]/..')

    #线上拉新活动
    def OnPullNew(self):
        self.click(xp,'//span[text()="添加"]/..')   #点击添加
        self.input(xp,'//input[@placeholder="请输入活动名称"]',)
        ONstattime =  '//td[@title="'+config.ONstartime +'"]'    #组合开始时间
        ONendtime = '//td[@title="'+config.ONendtime +'"]'       #组合结束时间
        self.click(xp,'//input[@placeholder="开始日期"]')
        self.click(xp,ONstattime)           
        self.click(xp,ONendtime)
        self.input(xp,config.OnPNpicture)   #插入图片
        self.click(xp,'//span[text()="提 交"]/..')

    #新用户奖励
    def UserRewards(self,):
        #判断是否有奖励
        if config.UserReward == 1:
            self.click(xp,'(//span[text()=" 有奖励 "])[1]/..')
            #添加奖项数
            if config.UserWardNum > 1 :
                for i in (config.UserWardNum-1) :
                    self.click(xp,'(//span[text()="奖励名称"])[1]/../../../../../../tbody/tr/td[2]/div/div/div/span/div/div[2]')     #添加奖项数量
                    self.AwardSetting(config.URtype,config.shuzhi,config.ASingles)
                    i += 1
            else :
                pass
        else:
            self.click(xp,'//span[text()="提 交"]')
            pass

#设计思路：通过a参数来定位每一条奖项的每一个元素的位置，用type来判断每一组数据的类型
    #奖励设置
    def AwardSetting(self,a,types,shuzhi,ASingles,coupons):
        choose = '//tr[@data-row-key="' + a + '"]/td/div/div/div/span/div/div/div'
        a = 0   #用来数据驱动时创建不同行的券
        Stored = '(//li[normalize-space(text())="储值"])'+'[' + a + ']' #储值路径
        integral = '(//li[normalize-space(text())="积分"])'+'[' + a + ']' #积分路径
        shuzhipath = '//tr[@data-row-key="' + a + '"]/td[2]/div/div/div/span/div/div/div/input'   #每条发放的路径
        reduction = '(//li[normalize-space(text())="满减券"])'+'[' + a + ']'        #满减券路径
        kims = '(//li[normalize-space(text())="代金券"])' +'[' + a + ']'        #代金券路径
        exchange = '(//li[normalize-space(text())="兑换券"])'+'[' + a + ']'     #兑换券路径
        Lucky_draw = '(//li[normalize-space(text())="抽奖机会"])'+'[' + a + ']'     #抽奖机会路径

        if  types == '储值' or '积分':
            self.click(xp,choose)
            if types == '储值' :
                self.click(xp,Stored)
            else :
                self.click(xp,integral)
            self.input(xp,shuzhipath,shuzhi) #积分、储值面额
        #判断类型
        else :
            self.click(xp,choose)
            if types == '满减券' :
                self.click(xp,reduction)
            elif types == '代金券':  
                self.click(xp,kims)
            elif types == '兑换券':
                self.click(xp,exchange)
            else :  
                self.click(xp,Lucky_draw)
            self.click(xp,'//tr[@data-row-key="' + a + '"]/td[2]/div/div/div/span/div/div/div/input')         #点击奖励 名称的输入框唤起选择券的窗口
            self.Prizelist(types,coupons)  #选择优惠券和抽奖机会
            self.input(xp,'//tr[@data-row-key="0"]td[3]/div/div/div/span/div/div[2]/ipnut',ASingles)      #单次发放的数量

    def submit(self):
        self.click(xp,'//span[text()="提 交"]')

    #邀请人奖励
    def InviterReward(self,a,AInvitation,CumulativeInvited):   #AInvitation单次邀请奖励   CumulativeInvited累计邀请奖励
        upperlimitpath = '//tr[@data-row-key="' + a + '"]/td[4]/div/div/div/span/div/div[2]/input'   #总数上限路径
        InviteNumpath = '//tr[@data-row-key="' + a + '"]/td[5]/div/div/div/span/div/div[2]/input'   #邀请数量路径
        #单次和累计邀请奖励组合
        if AInvitation == '无奖励' :
            if CumulativeInvited  == '有奖励':
                self.click(xp,'(//span[text()=" 有奖励 "])[2]')    #点击累计的有奖励
                self.AwardSetting(a,config.URtype,config.shuzhi,config.ASingles)  #奖项设置
                self.input(xp,upperlimitpath,config.upperlimit)
                self.input(xp,InviteNumpath,config.InviteNum)
            else:
                self.click(xp,'/span[text()="保 存"][2]/..')

        elif AInvitation == '有奖励' :
            self.click(xp,'(//span[text()=" 有奖励 "])[1]')    #点击单次的有奖励
            self.AwardSetting(a,config.URtype,config.shuzhi,config.ASingles)   #单次的奖项设置
            if CumulativeInvited  == '有奖励':
                self.click(xp,'(//span[text()=" 有奖励 "])[2]')     #点击累计的有奖励
                self.AwardSetting(a,config.URtype,config.shuzhi,config.ASingles)   #奖项设置
                self.input(xp,upperlimitpath,config.upperlimit)
                self.input(xp,InviteNumpath,config.InviteNum)
            else :
                self.click(xp,'/span[text()="保 存"][2]/..') 
        else :
            print('类型错误')

#新人送券
    def New_send_coupons(self,Name,scribe,options,startime,endtime):     #options 奖励个数
        self.click(xp,'//span[text()="添加"]/..')
        self.input(xp,'//div[@placeholder="请输入营销名称!"]/../input',Name)      #营销名称
        self.input(xp,'//div[@placeholder="请输入营销描述"]/../input',scribe)       #营销描述
        self.choosetime(startime,endtime)  #选择时间
        self.click(xp,'//span[normalize-space(text())="设置优惠"]')   #设置优惠
        if options > 1 :
            for i in options:
                self.click(xp,'//span[normalize-space(text())="设置优惠"]/../../../../../../div[2]/div/div/div/div/div/span[2]')
                i +=1      
        else :
            pass
        self.click(xp,'//span[text()="确 定"]')

    #选优惠券或者储值
    def CouponsOrIntegralOrStored(self,types,digits,C,name,denomination):   #digits奖励位数 denomination  面额
        self.digits = digits
        self.click(xp,'//div[@title="送储值"]/..')
        if types =='送积分' :
            self.click(xp,'(//div[@class="ant-row-flex"])[1]/div/div/span/span[1]/span/div/div/div')
            self.click(xp,'//li[normalize-space(text())="送积分"]')
        elif types == '送满减券' or '送代金券' or '送兑换券' or '抽奖机会' :
            if types =='抽奖机会' :
                self.click(xp,'//li[normalize-space(text())="抽奖机会"]')
                self.click(xp,'(//div[@class="ant-row-flex"])[1]/div/div/span/span[1]/span[2]')
                self.Prizelist(C,name)
            else:
                #选择类型点击选择优惠券
                if types == '送满减券':
                    self.click(xp,'//li[normalize-space(text())="送满减券"]')
                elif types == '送代金券' :
                    self.click(xp,'//li[normalize-space(text())="送代金券"]')
                else:
                    self.click(xp,'//li[normalize-space(text())="送兑换券"]')
                self.click(xp,'(//div[@class="ant-row-flex"])[1]/div/div/span/span[1]/span[2]')
                self.Prizelist(C,name)
        else:
            pass
        self.input(xp,'(//div[@class="ant-row-flex"])[1]/div/div/span/span[1]/span[2]/input',denomination)   # 设置积分和储值的面额

    #选择优惠券和抽奖活动
    def Prizelist(self,C,name):
        if len(name) != 0 :  #判断字符串长度不为零
            if C == '抽奖机会'or '送抽奖机会':
                self.click(xp,'//input[@placeholder="请输入抽奖名称"]',name)
            else:
                self.click(xp,'//input[@placeholder="请输入优惠券名称"]',name)
            self.click(xp,'//span[text()="查 询"]')
            self.click(xp,'//input[@placeholder="请输入优惠券名称"]/../../../../../../div/div/div/div/div/div[2]/div/div/table/tbody/tr/td[1]')
        else :
            self.click(xp,'(//a[text()="选择"])[1]')
        self.click(xp,'//div[text()="确定选择该优惠券？"]/../../div[2]/button[2]')
        self.click(xp,'//span[text()="确 定"]')


    #选完券后点击确定
    def determine(self):
            #点击确定
        self.click(xp,'(//span[text()="确 定"])')

